package bubblesn;

/**
 * Class to hold path lengths of Sorting Network.
 * @author Rafalmag
 */
public class PathLengths {

	// Following array is like hashtable.
	// Index is length,
	// value is number of paths of given length 
	private volatile Long[][] outputs;
	private int maxpath;
	private int inputs;

	/**
	 * Constructor
	 * @param inLines describes size of input
	 * @throws Exception when inLines<2
	 */		
	public PathLengths(int inLines){
		/*
		 * a_n = a_1+(n-1)*r
		 * a_n = -1+(n-1)*2 = 2*n-3
		 */
		maxpath=2*inLines-3+1;	// +1 because we count from "0"
		inputs = inLines;
		outputs= new Long[inputs][maxpath];
		for(int i=0; i< inLines; i++){
			for(int j=0; j<maxpath; j++){
				outputs[i][j]=0L;
			}
		}
	}
	
	/**
	 * Adds gates number to given output.
	 * @param output number of output
	 * @param gates number of gates to add
	 */
	public synchronized void addGatesNumToOutput(final int output, final int gates){
		outputs[output][gates]++;
	}
	
	/**
	 * 
	 * @return length of longest path in SN
	 */
	public int getMaxPath(){
		return maxpath;
	}
	
	/**
	 * 
	 * @param output number of output
	 * @param length 
	 * @return number of paths of given length at given output
	 */
	public long get(int output, int length){
		return outputs[output][length];
	}

}
